﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DelegatesAndEvents
{
    class DelegatesAndEvents
    {
        delegate double SeriesFunctionDelegate(int x);

        static double CalcSeries(SeriesFunctionDelegate func)
        {
            double currentValue = func(1);
            double sum = currentValue;
            double lastSum = sum;
            int x = 2;

            while (true)
            {
                currentValue = func(x);
                x++;
                sum += currentValue;

                if (Math.Abs(sum - lastSum) < 0.01)
                {
                    break;
                }
                lastSum = sum;
            }

            return sum;
        }

        static int Fact(int n)
        {
            int result = 1;
            for (int i = 2; i <= n; i++)
            {
                result += i;
            }
            return result;
        }

        static void Main(string[] args)
        {
            Console.WriteLine(CalcSeries(x => 1.0 / (x * x)));
            Console.WriteLine(CalcSeries(x => 1 / Math.Pow(2, x - 1)));
            Console.WriteLine(CalcSeries(x => 1.0 / Fact(x)));
        }
    }
}
